/*function ExpAsynch(shareinID) {
	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
	    }
	};
	xmlhttp.open("GET","isShareinID.do?shareinID=" + shareinID,true);
	xmlhttp.send();
}*/

var jq=jQuery.noConflict();

function load() {
	document.getElementById("shareinID").focus();
	drawGradiantBack('#a0e3f3');
	jq("#shareinIDrecovery").keyup(function(event){
	  if(event.keyCode == 13){
		  jq("#pwdrecoveryOK").click();
	  }
	});
	var leftSignIn;
	var leftLinkImg;
	var leftPwdrecoveryDiv;
	var leftPwdrecoveryBack;
	if(screen.width >= 800) {
		leftSignIn = (screen.width/1.82);
		leftLinkImg = (screen.width/1.7) - 360;
		leftPwdrecoveryDiv = (screen.width/1.7) - 540;
		leftPwdrecoveryBack = (screen.width/1.7) + 102;
	}
	else { 
		leftSignIn = 800/1.7;
		leftLinkImg = (950/1.7) - 360;
		leftPwdrecoveryDiv = (950/1.7) - 540;
		leftPwdrecoveryBack = (850/1.7) + 102;
	}
	document.getElementById("signin_form").style.left = leftSignIn+"px";
	document.getElementById("link_img").style.left = leftLinkImg+"px";
	document.getElementById("pwdrecoveryDiv").style.left = leftPwdrecoveryDiv+"px";
	document.getElementById("pwdrecovery_back").style.left = leftPwdrecoveryBack+"px";
	if(screen.width >= 850) leftSignIn = (screen.width/1.7);
	else leftSignIn = 850/1.7;
	document.getElementById("signup_form").style.left = leftSignIn+"px";
	
}

function pwdrecovery() {
	disablePage();
	jq("#link_img").hide();
	document.getElementById("link_img").style.visibility = "visible";
	jq("#link_img").slideDown("slow");
	jq("#pwdrecoveryDiv").hide();
	document.getElementById("pwdrecoveryDiv").style.visibility = "visible";
	jq("#pwdrecoveryDiv").slideDown("slow", function() {
		var x = document.getElementById("shareinIDrecovery");
		if(x) x.focus();
	});
}

function pwdrecoveryCancel() {
	jq("#link_img").slideUp("slow", function() {document.getElementById("link_img").style.visibility = "hidden";});
	jq("#pwdrecoveryDiv").slideUp("slow", function() {
		document.getElementById("pwdrecoveryDiv").style.visibility = "hidden";
		enablePage();
	});
}

function disablePage() {
	//Disable elements
	jq("#shareinID").attr('disabled', true);
	jq("#password").attr('disabled', true);
	jq("#persistent").attr('disabled', true);
	jq("#forgot").attr('onclick', '');
	jq("#forgot").hover(function(){
		jq(this).css({"cursor":"default","text-decoration":"underline"});
	},function(){jq(this).css("text-decoration","underline");});
	jq("#forgot").css({"cursor":"default","text-decoration":"underline"});
	jq("#signin_btn").attr('disabled', true);
	jq("#fullname").attr('disabled', true);
	jq("[name='gender']").attr('disabled', true);
	jq("#email").attr('disabled', true);
	jq("#new_shareinID").attr('disabled', true);
	jq("#new_password").attr('disabled', true);
	jq("#question").attr('disabled', true);
	jq("#answer").attr('disabled', true);
	jq("#signup_btn").attr('disabled', true);
	//Change colors
	document.getElementById("pwdrecovery_back").style.visibility = "visible";
	document.getElementById("header_backgroud").style.backgroundColor = "#aeaeae";
	document.getElementById("signin_btn").style.backgroundColor = "#d7d7d7";
	document.getElementById("signin_btn").style.color = "#707070";
	document.getElementById("signin_btn").style.borderColor = "#d7d7d7";
	document.getElementById("signup_btn").style.backgroundColor = "#d7d7d7";
	document.getElementById("signup_btn").style.color = "#707070";
	document.getElementById("signup_btn").style.borderColor = "#d7d7d7";
	document.getElementById("keep").style.color = "#d7d7d7";
	document.getElementById("signin_error").style.color = "#707070";
	document.getElementById("signin_error").style.borderColor = "#707070";
	document.getElementById("signin_error").style.backgroundColor = "#d7d7d7";
	document.getElementById("signup_error").style.color = "#707070";
	document.getElementById("signup_error").style.borderColor = "#707070";
	document.getElementById("signup_error").style.backgroundColor = "#d7d7d7";
	document.getElementById("join").style.color = "#707070";
	var labels = document.getElementsByTagName("label");
	for ( var i = 0; i < labels.length; i++) {
		if(i!=2) {
			var x = labels[i];
			x.style.color="#707070";
		}
	}
	var hr = document.getElementsByTagName("hr");
	for ( var i = 0; i < hr.length; i++) {
		if(i!=2) {
			var x = hr[i];
			x.style.borderColor="gray";
		}
	}
	drawGradiantBack('#cbcbcb');
}

function enablePage() {
	//PwdrecoveryDiv
	var recoveryDiv = document.getElementById("pwdrecoveryDiv");
	recoveryDiv.innerHTML = "<p style=\"color:#0f687b;font-size:1.2em;text-align:center;margin-top:30px;\">Please enter your <b>Sharein ID</b> :</p>" +
			"<input type=\"text\" name=\"shareinIDrecovery\" id=\"shareinIDrecovery\" style=\"margin-left:100px;\" /><br /><br />" +
			"<input type=\"button\" value=\"OK\" id=\"pwdrecoveryOK\" onclick=\"pwdrecoveryOK()\" />" +
			"<input type=\"button\" value=\"Cancel\" id=\"pwdrecoveryCancel\" onclick=\"pwdrecoveryCancel()\" />" +
			"<p id=\"pwdrecovery_error\"></p>";
	document.getElementById("pwdrecoveryOK").style.marginTop = "0px";
	document.getElementById("pwdrecovery_error").style.marginTop = "10px";
	jq("#shareinIDrecovery").keyup(function(event){
	  if(event.keyCode == 13){
		  jq("#pwdrecoveryOK").click();
	  }
	});
	//Enable elements
	jq("#shareinID").removeAttr('disabled');
	jq("#password").removeAttr('disabled');
	jq("#persistent").removeAttr('disabled');
	jq("#forgot").attr('onclick', 'pwdrecovery()');
	jq("#forgot").css("text-decoration","none");
	jq("#forgot").hover(function(){
		jq(this).css({"cursor":"pointer","text-decoration":"underline"});
	},function(){jq(this).css("text-decoration","none");});
	jq("#signin_btn").removeAttr('disabled');
	jq("#fullname").removeAttr('disabled');
	jq("[name='gender']").removeAttr('disabled');
	jq("#email").removeAttr('disabled');
	jq("#new_shareinID").removeAttr('disabled');
	jq("#new_password").removeAttr('disabled');
	jq("#question").removeAttr('disabled');
	jq("#answer").removeAttr('disabled');
	jq("#signup_btn").removeAttr('disabled');
	//Recover colors
	document.getElementById("pwdrecovery_back").style.visibility = "hidden";
	document.getElementById("header_backgroud").style.backgroundColor = "#70d5ee";
	document.getElementById("signin_btn").style.backgroundColor = "#b9ebf7";
	document.getElementById("signin_btn").style.color = "#1389a7";
	document.getElementById("signin_btn").style.borderColor = "#b9ebf7";
	document.getElementById("signup_btn").style.backgroundColor = "#b9ebf7";
	document.getElementById("signup_btn").style.color = "#1389a7";
	document.getElementById("signup_btn").style.borderColor = "#b9ebf7";
	document.getElementById("keep").style.color = "#d0f1f9";
	document.getElementById("signin_error").style.color = "red";
	document.getElementById("signin_error").style.borderColor = "red";
	document.getElementById("signin_error").style.backgroundColor = "#faf0e6";
	document.getElementById("signup_error").style.color = "red";
	document.getElementById("signup_error").style.borderColor = "red";
	document.getElementById("signup_error").style.backgroundColor = "#faf0e6";
	document.getElementById("join").style.color = "#1389a7";
	var labels = document.getElementsByTagName("label");
	for ( var i = 0; i < labels.length; i++) {
		if(i!=2) {
			var x = labels[i];
			x.style.color="#1389a7";
		}
	}
	var hr = document.getElementsByTagName("hr");
	for ( var i = 0; i < hr.length; i++) {
		if(i!=2) {
			var x = hr[i];
			x.style.borderColor="#c9e9f6";
		}
	}
	drawGradiantBack('#a0e3f3');
}

var pwdrecoveryShareinID;

function pwdrecoveryOK() {
	pwdrecoveryShareinID = document.getElementById("shareinIDrecovery").value;
	if(pwdrecoveryShareinID != "" && isShareinID(pwdrecoveryShareinID)) {
		var question = getAjax("GET", "getquestion.do", "shareinID=" + pwdrecoveryShareinID);
		var recoveryDiv = document.getElementById("pwdrecoveryDiv");
		recoveryDiv.innerHTML = "<p style=\"color:#0f687b;font-size:1.2em;text-align:center;margin-top:10px;\">Please enter your secret answer of <br/> your question :</p>" +
				"<table style=\"margin-top:-13px;margin-left:40px;\"><tr><td class=\"pwdrecovery_td\">Question :</td>" +
				"<td><input type=\"text\" name=\"pwdrecoveryQuestion\" id=\"pwdrecoveryQuestion\" readonly=\"readonly\" value=\"" + question + "\" /></td></tr><tr>" +
				"<td class=\"pwdrecovery_td\">Secret answer :</td><td><input type=\"text\" name=\"pwdrecoveryAnswer\" id=\"pwdrecoveryAnswer\"/></td></tr></table>" +
				"<input type=\"button\" value=\"OK\" id=\"pwdrecoveryOK\" onclick=\"pwdrecoveryOK2()\" />" +
				"<input type=\"button\" value=\"Cancel\" id=\"pwdrecoveryCancel\" onclick=\"pwdrecoveryCancel()\" />" +
				"<p id=\"pwdrecovery_error\"></p>";
		document.getElementById("pwdrecoveryOK").style.marginTop = "5px";
		document.getElementById("pwdrecovery_error").style.marginTop = "3px";
		document.getElementById("pwdrecoveryAnswer").focus();
		jq("#pwdrecoveryAnswer").keyup(function(event){
		  if(event.keyCode == 13){
			  jq("#pwdrecoveryOK").click();
		  }
		});
	}
	else {
		if(pwdrecoveryShareinID == "") document.getElementById("pwdrecovery_error").innerHTML = "Sharein ID must be filled out.";
		else document.getElementById("pwdrecovery_error").innerHTML = "Sharein ID doesn't exists.";
		jq("#pwdrecovery_error").hide();
		document.getElementById("pwdrecovery_error").style.visibility = "visible";
		jq("#pwdrecovery_error").show("fast");
	}
	return false;
}

function pwdrecoveryOK2() {
	var answer = getAjax("GET", "getanswer.do", "shareinID=" + pwdrecoveryShareinID);
	var answerValue = document.getElementById("pwdrecoveryAnswer").value;
	if(answer == answerValue) {
		var email = getAjax("GET", "getemail.do", "shareinID=" + pwdrecoveryShareinID);
		setAjax("GET", "sendemail.do", "shareinID=" + pwdrecoveryShareinID);
		var recoveryDiv = document.getElementById("pwdrecoveryDiv");
		recoveryDiv.innerHTML = "<p style=\"color:#0f687b;font-size:1.2em;text-align:center;margin-top:50px;\">&nbsp;An email containing your password is sent to <br/> your <b>" + email + "</b> account.</p>" +
				"<input type=\"button\" value=\"Finish\" id=\"pwdrecoveryFinish\" onclick=\"pwdrecoveryCancel()\" />";
	}
	else {
		document.getElementById("pwdrecovery_error").innerHTML = "Your answer is wrong !";
		jq("#pwdrecovery_error").hide();
		document.getElementById("pwdrecovery_error").style.visibility = "visible";
		jq("#pwdrecovery_error").show("fast");
	}
}

function drawGradiantBack(color) {
	var canvas = document.getElementById("footerSpaceCanvas");
    if (canvas.getContext) {
    	var ctx = canvas.getContext("2d");
    	ctx.clearRect (0,0,400,147);
    	// Create gradients
    	var lineargradient = ctx.createLinearGradient(0,0,0,150);
    	lineargradient.addColorStop(0,'white');
    	lineargradient.addColorStop(1,color);
    	// assign gradients to fill and stroke styles
    	ctx.fillStyle = lineargradient;
    	// draw shapes
    	ctx.fillRect(0,0,400,147);
    }
}

function setAjax(method, url, str) {
	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if(method == "GET") {
		xmlhttp.open(method,url+"?"+str,true);
		xmlhttp.send();
	}
	else { // POST
		xmlhttp.open(method,url,true);
		xmlhttp.send(str);
	}
}

function getAjax(method, url, str)
{
	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if(method == "GET") {
		xmlhttp.open(method,url+"?"+str,false);
		xmlhttp.send();
	}
	else { // POST
		xmlhttp.open(method,url,false);
		xmlhttp.send(str);
	}
	return xmlhttp.responseText;
}

function isShareinID(shareinID) {
	var resp = getAjax("GET", "isShareinID.do", "shareinID=" + shareinID);
	if(resp == "true") return true;
	else return false;
}

function isEmail(email) {
	var atpos=email.indexOf("@");
	var dotpos=email.lastIndexOf(".");
	if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length) return false;
	return true;
}

function validateSignupForm() {
	var fullname = document.forms["signup_form"]["fullname"].value;
	var gender = document.forms["signup_form"]["gender"].value;
	var email = document.forms["signup_form"]["email"].value;
	var new_shareinID = document.forms["signup_form"]["new_shareinID"].value;
	var new_password = document.forms["signup_form"]["new_password"].value;
	var question = document.forms["signup_form"]["question"].value;
	var answer = document.forms["signup_form"]["answer"].value;
	if(fullname == "") {
		document.forms["signup_form"]["fullname"].focus();
		var errortag = document.getElementById("signup_error");
		errortag.innerHTML = "Fullname must be filled out";
		errortag.style.marginTop = "35px";
		errortag.style.marginBottom = "-15px";
		errortag.style.padding = "10px";
		jq("#signup_error").hide();
		document.getElementById("signup_error").style.visibility = "visible";
		jq("#signup_error").show("fast");
		return false;
	}
	else if(gender == "null") {
		document.forms["signup_form"]["gender"].focus();
		var errortag = document.getElementById("signup_error");
		errortag.innerHTML = "Gender must be choosen";
		errortag.style.marginTop = "35px";
		errortag.style.marginBottom = "-15px";
		errortag.style.padding = "10px";
		jq("#signup_error").hide();
		document.getElementById("signup_error").style.visibility = "visible";
		jq("#signup_error").show("fast");
		return false;
	}
	else if(!isEmail(email)) {
		document.forms["signup_form"]["email"].focus();
		var errortag = document.getElementById("signup_error");
		errortag.innerHTML = "Not a valid e-mail address";
		errortag.style.marginTop = "35px";
		errortag.style.marginBottom = "-15px";
		errortag.style.padding = "10px";
		jq("#signup_error").hide();
		document.getElementById("signup_error").style.visibility = "visible";
		jq("#signup_error").show("fast");
		return false;
	}
	else if(new_shareinID == "0" || new_shareinID == "" || isShareinID(new_shareinID)) {
		document.forms["signup_form"]["new_shareinID"].focus();
		var errortag = document.getElementById("signup_error");
		if(new_shareinID == "0") errortag.innerHTML = "Sharein ID must be different from 0";
		else if(new_shareinID == "") errortag.innerHTML = "Sharein ID must be filled out";
		else errortag.innerHTML = "Sharein ID already exists. Please choose another one";
		errortag.style.marginTop = "35px";
		errortag.style.marginBottom = "-15px";
		errortag.style.padding = "10px";
		jq("#signup_error").hide();
		document.getElementById("signup_error").style.visibility = "visible";
		jq("#signup_error").show("fast");
		return false;
	}
	else if(new_password.length < 4) {
		document.forms["signup_form"]["new_password"].focus();
		var errortag = document.getElementById("signup_error");
		errortag.innerHTML = "Password length must be more than 3";
		errortag.style.marginTop = "35px";
		errortag.style.marginBottom = "-15px";
		errortag.style.padding = "10px";
		jq("#signup_error").hide();
		document.getElementById("signup_error").style.visibility = "visible";
		jq("#signup_error").show("fast");
		return false;
	}
	if(question == "") {
		document.forms["signup_form"]["question"].focus();
		var errortag = document.getElementById("signup_error");
		errortag.innerHTML = "Sercret question must be filled out";
		errortag.style.marginTop = "35px";
		errortag.style.marginBottom = "-15px";
		errortag.style.padding = "10px";
		jq("#signup_error").hide();
		document.getElementById("signup_error").style.visibility = "visible";
		jq("#signup_error").show("fast");
		return false;
	}
	if(answer == "") {
		document.forms["signup_form"]["answer"].focus();
		var errortag = document.getElementById("signup_error");
		errortag.innerHTML = "Answer must be filled out";
		errortag.style.marginTop = "35px";
		errortag.style.marginBottom = "-15px";
		errortag.style.padding = "10px";
		jq("#signup_error").hide();
		document.getElementById("signup_error").style.visibility = "visible";
		jq("#signup_error").show("fast");
		return false;
	}
	return true;
}

function isValidPassword(shareinID, password) {
	var resp = getAjax("GET", "isValidPassword.do", "shareinID=" + shareinID + "&password=" + password);
	if(resp == "true") return true;
	else return false;
}

function validateSigninForm() {
	var shareinID = document.forms["signin_form"]["shareinID"].value;
	var password = document.forms["signin_form"]["password"].value;
	if(shareinID == "" || password == "" || !isShareinID(shareinID) || !isValidPassword(shareinID, password)) {
		var errortag = document.getElementById("signin_error");
		errortag.innerHTML = "Not a valid Sharein ID or Password";
		errortag.style.padding = "5px";
		errortag.style.marginTop = "-5px";
		jq("#signin_error").hide();
		document.getElementById("signin_error").style.visibility = "visible";
		jq("#signin_error").show("fast");
		return false;
	}
	//return true;
}